package com.molyfun.parents.activity.ask.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.rest.bean.LikeResponse;
import com.molyfun.parents.rest.bean.TopicBriefBean;
import com.molyfun.parents.utils.EmptyHelper;
import com.molyfun.parents.utils.LikeAnimation;
import com.molyfun.parents.utils.UIUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class TopicOperationView extends LinearLayout implements View.OnClickListener{

    protected LayoutInflater layoutInflater;

    private Context context;

    private String mTopicDetailId;

    @InjectView(R.id.view_icon)
    ImageView mViewIcon;
    @InjectView(R.id.view_count)
    TextView mViewCount;

    @InjectView(R.id.like_icon)
    ImageView mLikeIcon;
    @InjectView(R.id.like_count)
    TextView mLikeCount;


    @InjectView(R.id.comment_count)
    TextView mCommentCount;

    /**
     * 不缓存整个Model，没必要
     */
    private int mCurrentLikeCount;
    private String mLikeId;//缓存赞后获取的id

    private int mCurrentOperationType = OperationType.TYPE_TOPIC;

    public interface OperationType{
        int TYPE_TOPIC = 1001;
        int TYPE_HOMELAND = 1002;
    }

    public TopicOperationView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public TopicOperationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TopicOperationView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_topic_operation_bar, this);
        ButterKnife.inject(this);
    }

    public void initData(TopicBriefBean topicDetailBean, int category, int type) {
        mCurrentOperationType = type;
        mTopicDetailId = topicDetailBean.getId();
        mLikeId = topicDetailBean.getLikeid();
        if(!EmptyHelper.isTopicDetailPublic(topicDetailBean) && !(mCurrentOperationType == OperationType.TYPE_HOMELAND)){
            mViewIcon.setVisibility(View.INVISIBLE);
            mViewCount.setVisibility(View.INVISIBLE);
            mLikeIcon.setVisibility(View.INVISIBLE);
            mLikeCount.setVisibility(View.INVISIBLE);
        }else{
            mViewCount.setText(getBriefStr(topicDetailBean.getOpentimes()));
            if(mCurrentOperationType == OperationType.TYPE_HOMELAND){
                mCurrentLikeCount = topicDetailBean.getPraisetimes();
            }else{
                mCurrentLikeCount = topicDetailBean.getLiketimes();
            }
            mLikeCount.setText(getBriefStr(mCurrentLikeCount));
            mLikeCount.setOnClickListener(this);
            mLikeIcon.setOnClickListener(this);
            if(TextUtils.isEmpty(mLikeId)){
                mLikeIcon.setImageResource(R.mipmap.ic_topic_cell_dislike);
            }else{
                mLikeIcon.setImageResource(R.mipmap.ic_topic_cell_like);
            }
        }
        if(category == AskTopBar.AskBarChooseTab.BAR_MINE ){
            mCommentCount.setText(getBriefStr(topicDetailBean.getReplytimes()));
        }else{
            mCommentCount.setText(getBriefStr(topicDetailBean.getCommenttimes()));
        }
    }

    private String getBriefStr(int openTimes){
        if(openTimes < 10000){
            return openTimes + "";
        }else{
            return (openTimes / 10000) + (".") + ((openTimes / 1000) % 10) + "万" ;
        }
    }

    /**
     * init data
     * and associated to TopicDetailBean
     */
    public void initData(TopicBriefBean topicDetailBean, int category) {
        initData(topicDetailBean, category, OperationType.TYPE_TOPIC);
    }

    public void setReplyCountRed(boolean isRed){
        if(isRed){
            mCommentCount.setTextColor(ContextCompat.getColor(getContext(), R.color.color_base));
        }else{
            mCommentCount.setTextColor(ContextCompat.getColor(getContext(), R.color.color_cell_text_title));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.like_icon:
            case R.id.like_count:
                if(TextUtils.isEmpty(mLikeId)){
                    likeTopic();
                }else{
                    cancelLikeTopic();
                }
//                if(!TextUtils.isEmpty(mCommentInput.getText().toString().trim())){
//                    sendComment(mCommentInput.getText().toString().trim());
//                }
                break;
        }
    }

    private void likeTopic(){
        Call<LikeResponse> call;
        if(mCurrentOperationType == OperationType.TYPE_TOPIC){
            call = MFPApplication.getInstance().getRestClient().likeTopic(mTopicDetailId);
        }else{
            call = MFPApplication.getInstance().getRestClient().likeHomeLand(mTopicDetailId);
        }

        call.enqueue(new Callback<LikeResponse>() {
            @Override
            public void onResponse(Call<LikeResponse> call, Response<LikeResponse> response) {
                if (response.body().getCode() == 200) {
                    mLikeId = response.body().getData().getId();
                    mLikeIcon.setImageResource(R.mipmap.ic_topic_cell_like);
                    mLikeIcon.startAnimation(new LikeAnimation(false, 1.8f, 0.8f, 1.0f));
                    mCurrentLikeCount ++;
                    mLikeCount.setText(mCurrentLikeCount + "");
                }
            }

            @Override
            public void onFailure(Call<LikeResponse> call, Throwable t) {
                UIUtils.showToast(getContext(), "赞失败");
            }
        });
    }

    private void cancelLikeTopic(){
        if(TextUtils.isEmpty(mLikeId)){
            return;
        }

        Call<BaseResponse> call;
        if(mCurrentOperationType == OperationType.TYPE_TOPIC){
            call = MFPApplication.getInstance().getRestClient().cancelLikeTopic(mLikeId);
        }else{
            call = MFPApplication.getInstance().getRestClient().cancelLikeHomeLand(mLikeId);
        }

        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                if (response.body().getCode() == 200) {
                    mLikeIcon.setImageResource(R.mipmap.ic_topic_cell_dislike);
                    mCurrentLikeCount --;
                    mLikeCount.setText(mCurrentLikeCount + "");
                    mLikeId = null;
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                UIUtils.showToast(getContext(), "赞失败");
            }
        });
    }



}